package com.yangyuhao.haowuyoupin;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDialog;
import com.yangyuhao.haowuyoupin.db.UserDao;
import com.yangyuhao.haowuyoupin.model.User;

public class LoginActivity extends AppCompatActivity {
    private UserDao userDao;
    private EditText etAccount;
    private EditText etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        userDao = new UserDao(this);
        
        etAccount = findViewById(R.id.et_account);
        etPassword = findViewById(R.id.et_password);

        // 登录按钮点击事件
        findViewById(R.id.btn_login).setOnClickListener(v -> {
            String account = etAccount.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            if (TextUtils.isEmpty(account) || TextUtils.isEmpty(password)) {
                Toast.makeText(this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            // 验证登录
            if (userDao.verifyLogin(account, password)) {
                // 更新登录状态
                userDao.updateLoginStatus(account, true);
                
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                // 跳转到主页面
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(this, "账号或密码错误", Toast.LENGTH_SHORT).show();
            }
        });

        // 注册按钮点击事件
        findViewById(R.id.btn_register).setOnClickListener(v -> {
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
        });
    }
} 